<?php

class Worldlevel{

	public $money;

	public $order;

	public $site_id;

	public $userlist;

	public $orderdb;

	public $memberdb;

	public $member_id;

	public $error = array();

	//构造函数

	public function Worldlevel($order_id = 0, $site_id = 0, $member_id = 0){





		
		$this->memberdb = D('Member');

		$this->orderdb = D('Order');

		$this->saledb = M('Sale');

		$this->money = $this->getmoney();

		$this->site_id = $site_id;

		

		$db = M('Affiliatesetting');

		$affsetting = $db -> where(array('site_id' => $this->site_id,'classname' => 'Worldlevel')) -> find();

		$this -> affsettingarr = unserialize($affsetting['value']);

		

		$this->order = $this->orderdb -> where(array('id' => $order_id)) -> find();

		

		

		

		$this->updateUserLevel();

	}

	//创建设置项表单

	public function createform(){
	$db = M('Affiliatesetting');
	//	exit();
 $ret = $db->where(array(
            
            'site_id' => $this->site_id,
            
            'classname' => 'Worldlevel'
        ))->find();
       
        $ret = unserialize($ret['value']);
        
        // F('ret',$ret);
        
        $html = '<style>#Worldlevel td{padding:5px;}</style>

		<style>

		body{ background-color:#5da9c0}

		 table{ background-color:#639d75;border:1px solid #79304e}

		 input{ background-color:#b7d2b3 }

		 </style>

		<table    id="Worldlevel"  border="1" cellspacing="0" cellpadding="5" width="100%">

  <tr>

    <th align="center" style="text-align:center; width:100px;">级别</th>

	<th align="center" style="text-align:center;">级别名称</th>

	<th align="center" style="text-align:center;">直接下级</th>

  </tr>

  <tr>

    <td align="center" valign="middle">1</td>

	<td align="center" valign="middle">《消费者》&nbsp;&nbsp;<input type="text" name="levelname[]" value="' . $ret['levelname'][0] . '" /></td>

<td></td>

  </tr>

  <tr>

    <td align="center" valign="middle">2</td>

	<td align="center" valign="middle"> 《 商 家 》&nbsp;&nbsp;<input type="text" name="levelname[]" value="' . $ret['levelname'][1] . '" /></td>

	<td></td>

  </tr>

  <tr>

    <td align="center" valign="middle">3</td>

	<td align="center" valign="middle">《市代理》&nbsp;&nbsp;<input type="text" name="levelname[]" value="' . $ret['levelname'][2] . '" /></td>

<td></td>

  </tr>

   <tr>

    <td align="center" valign="middle">4</td>

	<td align="center" valign="middle">《省代理》&nbsp;&nbsp;<input type="text" name="levelname[]" value="' . $ret['levelname'][3] . '" /></td>



<td></td>

 </tr>

   <tr>

    <td align="center" valign="middle">5</td>

	<td align="center" valign="middle">《区代理》&nbsp;&nbsp;<input type="text" name="levelname[]" value="' . $ret['levelname'][4] . '" /></td>

<td></td>

  </tr>

<tr>

    <td align="center" valign="middle">6</td>

	<td align="center" valign="middle">《服务商》&nbsp;&nbsp;<input type="text" name="levelname[]" value="' . $ret['levelname'][5] . '" /></td>

<td align="center" valign="middle">《业务员》<input type="text" name="levelname[]" value="' . $ret['levelname'][6] . '" /></td>

  </tr>

</table>

<br>

<table cellpadding="0" class="Threelevel" cellspacing="0" border="1" width="100%">

    <td align="center" valign="middle" style="width:150px;">会员升级后通知信息</td>

    <td align="left" colspan="2"><input style="width:100%" type="text" name="changelevel" value="' . $ret['changelevel'] . '" /><br>

等级名称@levelname@

</td>

  </tr>

</table>

<style>.Threelevel td{padding:5px;}</style>

<div style="margin-top:35px;">

<table class="Threelevel" border="1" cellspacing="0" style=" float: left;" cellpadding="5" width="40%">

  <tr>

    <th align="center" style="text-align:center;  width:100px;" colspan="2">消费者</th>

  </tr>

    <tr>

  <th align="center" style="text-align:center; width:100px;">一级</th>

	<th align="center" style="text-align:center; width:100px;">二级</th>

  </tr>

  <tr>

    <td align="center"><input type="text" name="first[]" value="' . $ret['first'][0] . '" />%</td>

	<td align="center"><input type="text" name="second[]" value="' . $ret['second'][0] . '" />%</td>

  </tr>

</table>

<table class="Threelevel" border="1" cellspacing="0" style=" float: right;" cellpadding="5" width="40%">

  <tr>

    <th align="center" style="text-align:center; width:100px;" colspan="2">商家</th>

  </tr>

  <tr>

  <th align="center" style="text-align:center; width:100px;">一级</th>

	<th align="center" style="text-align:center; width:100px;">二级</th>

  </tr>

  <tr>

    <td align="center"><input type="text" name="first[]" value="' . $ret['first'][1] . '" />%</td>

	<td align="center"><input type="text" name="second[]" value="' . $ret['second'][1] . '" />%</td>

  </tr>

</table>

</div>

<div style="margin-top:165px;">

<table class="Threelevel" border="1"  style=" float:left;" cellspacing="0" cellpadding="5" width="40%">

  <tr>

    <th align="center" style="text-align:center; width:100px;" colspan="2">省代理</th>

  </tr>

  <tr>

  <tr>

<th align="center" style="text-align:center; width:100px;">16%系列</th>

	<th align="center" style="text-align:center; width:100px;">8%系列</th>

  </tr>

    <td align="center"><input type="text" name="first[]" value="' . $ret['first'][2] . '" />%</td>

	<td align="center"><input type="text" name="second[]" value="' . $ret['second'][2] . '" />%</td>

  </tr>

</table>

<table class="Threelevel" border="1"  style=" float: right;" cellspacing="0" cellpadding="5" width="40%">

  <tr>

    <th align="center" style="text-align:center; width:100px;" colspan="2">市代理</th>

  </tr>

  <tr>

<th align="center" style="text-align:center; width:100px;">16%系列</th>

	<th align="center" style="text-align:center; width:100px;">8%系列</th>

  </tr>

   <tr>

    <td align="center"><input type="text" name="first[]" value="' . $ret['first'][3] . '" />%</td>

	<td align="center"><input type="text" name="second[]" value="' . $ret['second'][3] . '" />%</td>

  </tr>

</table>

</div>

<div style="margin-top:285px;">

<table class="Threelevel" border="1" cellspacing="0"  cellpadding="5" width="40%">

  <tr>

    <th align="center" style="text-align:center; width:100px;" colspan="2">区代理</th>

  </tr>

    <tr>

<th align="center" style="text-align:center; width:100px;">16%系列</th>

	<th align="center" style="text-align:center; width:100px;">8%系列</th>

  </tr>

  <tr>

    <td align="center"><input type="text" name="first[]" value="' . $ret['first'][4] . '" />%</td>

	<td align="center"><input type="text" name="second[]" value="' . $ret['second'][4] . '" />%</td>

  </tr>

</table>

<div>

<br /><br />

<table class="Threelevel" border="1" cellspacing="0" cellpadding="5" width="70%">



  <tr>

    <th align="center" style="text-align:center; width:100px;" colspan="4">服务商</th>

  </tr>

<tr>

  <th align="center" style="text-align:center; width:100px;" colspan="2">一级</th>



	<th align="center" style="text-align:center; width:100px;" colspan="2">二级</th>

</tr> 

  <tr>

    <td style="text-align:center;"rowspan="2" >直推商家：</td><td align="center" rowspan="2" ><input type="text" name="first[]" value="' . $ret['first'][5] . '" />%</td>



	<td style="text-align:center;" rowspan="3" >直推服务商个数：</td><td align="center"> 《 1 - 9 个 》<input type="text" name="second[]" value="' . $ret['second'][5] . '" />%</td>

  </tr>

  <tr>

	<td align="center">《10-29个》<input type="text" name="second[]" value="' . $ret['second'][6] . '" />%</td>

  </tr>

  <tr>

 <td style="text-align:center;" >直接下级：</td><td align="center" rowspan="3" ><input type="text" name="first[]" value="' . $ret['first'][6] . '" />%</td>

	<td align="center">《30以上》<input type="text" name="second[]" value="' . $ret['second'][7] . '" />%</td>

  </tr>

</table>

';
        
        return $html;

	}

	

	//保存设置项

	public function saveform(){

		

			$data['classname'] = 'Worldlevel';

			$data['value'] = serialize($_POST);

			$data['site_id'] = $this->site_id;

			$db = M('Affiliatesetting');



			//dump($data);

			$ret = $db -> where(array('site_id' => $data['site_id'],'classname' => $data['classname'])) -> find();

			if($ret){

				$saveret = $db -> where(array('site_id' => $data['site_id'],'classname' => $data['classname'])) -> data(array('value' => $data['value'])) -> save();

			}else{

				$saveret = $db -> data($data) -> add();

			}

			if($saveret){

				return true; 

			}else{

				$this->error['info'] = $db -> getLastSql();

				return false;

			}

			

		

	}

	
 public function member_info($id)
    {
        return $this->member_db->where('id = ' . $id)->find();
    }

    public function order()
    {
        $this->order = $tihs->order_db->where('id = ' . $this->orderid)->find();
    }

    public function auto_save()
    {
        
    }

    public function get_user()
    {
		
		
	}

   

    public function get_money($level)
    {
	//return $this->order[];	
		
	}

    public function get_relation($member, $pid)
    {
        $map['member_id'] = $member;
        $map['supplier_id'] = $pid;
        return M('Member_connect')->where($map)
            ->filed('type')
            ->find();
    }

    public function calculate($data)
    {
        $menoy_db = M('Member_menoy');
        $res = $menoy_db->where('member_id = ' . $data['member_id'])->find();
        if (! $res) {
            return M('Member_menoy')->add($data);
        } else {
            $data['online'] = $res['online'] + $data['online'];
            $data['offline'] = $res['offline'] + $data['offline'];
            $data['put'] = $res['put'] + $data['put'];
            $data['save'] = $res['save'] + $data['save'];
            $data['total'] = $res['total'] + $data['online'] + $data['offline'] + $data['put'] + $data['save'];
            return $menoy_db->where('member_id = ' . $data['member_id'])->save($data);
        }
    }

   	

	public function updateUserLevel(){

		

	
	}

	

	//获取等级名称

	public function getLevelName(){

		$memberInfo = $this->memberdb -> where(array('id' => $this->member_id)) -> find();

		$namelist = $this -> affsettingarr['levelname'];

		$namelist[] = $this -> affsettingarr['levelnamebase'];

		$namelist[] = '普通会员';

		$namelist = array_reverse($namelist);

		return $namelist[$memberInfo['level']];

	}

	

	//获取等级名称列表

	public function getLevelNameList(){

		$memberInfo = $this->memberdb -> where(array('id' => $this->member_id)) -> find();

		$namelist = $this -> affsettingarr['levelname'];

		$namelist[] = $this -> affsettingarr['levelnamebase'];

		$namelist[] = '普通会员';

		$namelist = array_reverse($namelist);

		return $namelist;

	}

	

	//获取父子级会员数组2级

	   public function getPid($pid = 0, $arr = array(), $deep = 0)
    
    {
        if ($pid == 0) {
            $pid = $this->member_id;
        }
        $re = M('Member')->where(array(
            'id' => $pid,
            'site_id' => __SITEID__
        ))
            ->field('level,pid,openid')
            ->find();
        if ($deep == 2 || $re == null) {
            return $arr;
        }
        if (! is_array($re)) {
            return $arr;
        } else {
            $re['deep'] = $deep + 1;
            $arr[] = $re;
            $deep ++;
            return $this->getPid($re['pid'], $arr, $deep);
        }
    }

	

	

	

	

	

	

	//获取VIP分成用户列表(需要分成的用户列表)

	public function getuserlist(){

		$ordermember = $this->memberdb -> where(array('id' => $this->order['member_id'])) -> find();

		

		$affsettingarr = $this -> affsettingarr;

		

		$list = $this -> getUserArr($ordermember['pid']);

		foreach($list as $k => $v){

			if($v['level'] > 0){

			    $list[$k]['aff'] = $affsettingarr['level'][$k];

			}else{

				$list[$k]['aff'] = 0;

			}

		}

		return $list;

	}

	

	//获取总代分成总额

	public function getuserlistAll(){

		$ret = array();

		$ordermember = $this->memberdb -> where(array('id' => $this->member_id)) -> find();

		$list = $this -> getUserArrAll($ordermember['pid']);

		$affList = array_reverse($this->affsettingarr['rlevel']);//dump($affList);

		foreach($list as $k => $v){

			

			if($k == 0){

				$aff = $affList[$v['level']-2];

			}else{

				$aff = $affList[$v['level']-2] - $list[$k - 1]['aff'];

			}

			if($aff > 0){

			    $list[$k]['aff'] = $aff;

				

				$ret[] = $list[$k];

			}

			//$list[$k]['aff'] = $affList[$v['level']-2];

		}

		return $ret;

	}

	

	//写入分成日志

	

	public function insertlogAll(){

		$db = M('Member_account_log');

		$productList = $this->getmoney();

		$userList = $this->getuserlistAll();

		$domain = C('DOMAIN_MAIN');

		$picurl = 'http://'.$domain[0].'/Public'.M('Product') -> where(array('id' => $productList[0]['product_id'])) -> getField('img_url');

		foreach($userList as $k => $v){

			$money = 0;

			foreach($productList as $key => $val){

				

				

				$map['member_id'] = $v['id'];

				$map['order_id'] = $this->order['id'];

				$map['product_id'] = $val['product_id'];

				if($db -> where($map) -> find()){

					continue;

				}

				

				

				$data['member_id'] = $v['id'];

				$data['user_id'] = 0;

				$data['pdstate'] = 3; //类型3为分成

				$data['dateline'] = time();

				$data['changenum'] = round($val['aff_money']*$v['aff']/100,2);

				$data['order_id'] = $this->order['id'];

				$data['order_sn'] = $this->order['ordernumber'];

				$data['product_id'] = $val['product_id'];

				$data['num'] = $val['num'];

				$data['site_id'] = $this->site_id;

				$data['attr'] = $val['attr'];

				$data['remark'] = '粉丝下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$data['changenum'];

				$data['goodsMoney'] = $val['total'];

				if($data['changenum'] > 0){

					$money += $data['changenum'];

					$db -> data($data) -> add();

				}

			}

			if($money > 0){

				//信息通知

				$sendWXNews[0]['title'] = '会员下单通知';

				$sendWXNews[0]['url'] = '';

				$sendWXNews[0]['picurl'] = $picurl;

				$sendWXNews[0]['description'] = '粉丝下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$money;

				//$msg = '粉丝下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$money;

				sendWXNews($v['openid'], $sendWXNews);

			}

		}

	}

	

	

	public function insertlog(){

		$db = M('Member_account_log');

		$productList = $this->getmoney();

		$userList = $this->getuserlist();

		$domain = C('DOMAIN_MAIN');

		$picurl = 'http://'.$domain[0].'/Public'.M('Product') -> where(array('id' => $productList[0]['product_id'])) -> getField('img_url');

		foreach($userList as $k => $v){

			$money = 0;

			foreach($productList as $key => $val){

				

				

				$map['member_id'] = $v['id'];

				$map['order_id'] = $this->order['id'];

				$map['product_id'] = $val['product_id'];

				if($db -> where($map) -> find()){

					continue;

				}

				

				

				$data['member_id'] = $v['id'];

				$data['user_id'] = 0;

				$data['pdstate'] = 3; //类型3为分成

				$data['dateline'] = time();

				$data['changenum'] = round($val['aff_money']*$v['aff']/100,2);

				$data['order_id'] = $this->order['id'];

				$data['order_sn'] = $this->order['ordernumber'];

				$data['product_id'] = $val['product_id'];

				$data['num'] = $val['num'];

				$data['site_id'] = $this->site_id;

				$data['attr'] = $val['attr'];

				$data['remark'] = '粉丝下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$data['changenum'];

				$data['goodsMoney'] = $val['total'];

				if($data['changenum'] > 0){

					$money += $data['changenum'];

					$db -> data($data) -> add();

				}

			}

			if($money > 0){

				//信息通知

				$sendWXNews[0]['title'] = '会员下单通知';

				$sendWXNews[0]['url'] = '';

				$sendWXNews[0]['picurl'] = $picurl;

				$sendWXNews[0]['description'] = '粉丝下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$money;

				//$msg = '粉丝下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$money;

				sendWXNews($v['openid'], $sendWXNews);

			}

		}

		//总代分成写入

		$this->insertlogAll();

	}

	

	//撤消一个分成

	public function loggoback(){

		

	}

	

	//获取分成总额

	public function getmoney(){

		return $this -> saledb -> where(array('order_id' => $this->order['id'])) -> Field('(aff_money*num) as aff_money,product_id,total,num,attr') -> select();

	}

	public function sendMsg(){}

	//返回错误信息

	public function getError(){

		return $this->error['info'];

	}

}